home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / sys / amiga / old / amiwn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  10.3 KB  |  455 lines

  1. /* $Id: amiwn.c,v 1.1 1993/03/15 21:30:49 mjl Exp $
  2.    $Log: amiwn.c,v $
  3.  * Revision 1.1  1993/03/15  21:30:49  mjl
  4.  * Files shuffled around in the Amiga driver reorganization.
  5.  *
  6.  * Revision 1.2  1992/10/12  17:11:20  mjl
  7.  * Amiga-specific mods, including ANSI-fication.
  8.  *
  9.  * Revision 1.1  1992/05/20  21:35:23  furnish
  10.  * Initial checkin of the whole PLPLOT project.
  11.  *
  12. */
  13.  
  14. /*    amiwn.c
  15.  
  16.     PLPLOT Amiga window device driver.
  17. */
  18.  
  19. #include "plplot.h"
  20. #include <stdio.h>
  21. #include "dispatch.h"
  22. #include "plamiga.h"
  23.  
  24. /* Function prototypes */
  25.  
  26. static void beepw(void);
  27. static void setpen(PLINT);
  28.  
  29. /* top level declarations */
  30.  
  31. PLINT XOffset, YOffset, PLWidth, PLHeight;
  32. PLINT MaxPLWidth, MaxPLHeight;
  33. PLINT InitPLWidth, InitPLHeight;
  34. extern PLINT MaxColors;
  35. static PLINT CurColor;
  36.  
  37. FILE *PlotFile;
  38. short fbuffer;
  39.  
  40. /* (dev) will get passed in eventually, so this looks weird right now */
  41.  
  42. static PLDev device;
  43. static PLDev (*dev) = &device;
  44.  
  45. /*----------------------------------------------------------------------*\
  46. * amiwn_init()
  47. *
  48. * Initialize device.
  49. \*----------------------------------------------------------------------*/
  50.  
  51. void
  52. amiwn_init(PLStream *pls)
  53. {
  54.     PLFLT Initdpmx, Initdpmy;
  55.     
  56.     pls->termin = 1;        /* is an interactive terminal */
  57.     pls->icol = 1;
  58.     pls->width = 1;
  59.     pls->bytecnt = 0;
  60.     pls->page = 0;
  61.  
  62.     /* Scan defaults file */
  63.  
  64.     GetPLDefs();
  65.  
  66.     /* Open the required libraries. */
  67.  
  68.     OpenLibs();
  69.  
  70.     /* Open window for graphics */
  71.  
  72.     OpenPLWind();
  73.  
  74.     /* Set up virtual screen width and height */
  75.     /* Virtual screen is 25 times the actual one. */
  76.  
  77.     MaxPLWidth = PLScreen->Width - PLWindow->BorderLeft - PLWindow->BorderRight - 16;
  78.     MaxPLHeight = PLScreen->Height - PLWindow->BorderTop - PLWindow->BorderBottom - 16;
  79.     InitPLWidth = MaxPLWidth * 25;
  80.     InitPLHeight = MaxPLHeight * 25;
  81.     Initdpmx = GfxBase->NormalDPMX;
  82.     Initdpmy = GfxBase->NormalDPMY;
  83.     if (PLScreen->ViewPort.Modes & HIRES)
  84.     Initdpmx *= 2.;
  85.     if (PLScreen->ViewPort.Modes & LACE)
  86.     Initdpmy *= 2.;
  87.  
  88. /* Set up device parameters */
  89.  
  90.     dev->xold = UNDEFINED;
  91.     dev->yold = UNDEFINED;
  92.     dev->xmin = 0;
  93.     dev->xmax = InitPLWidth -1;
  94.     dev->ymin = 0;
  95.     dev->ymax = InitPLHeight -1;
  96.  
  97.     setpxl((PLFLT) (Initdpmx / 40.), (PLFLT) (Initdpmy / 40.));
  98.     setphy(0, (InitPLWidth - 1), 0, (InitPLHeight - 1));
  99.  
  100.     SetAPen(PLWRPort, 1);
  101.     SetDrMd(PLWRPort, JAM1);
  102.     setlimits();
  103. }
  104.  
  105. /*----------------------------------------------------------------------*\
  106. * amiwn_line()
  107. *
  108. * Draw a line in the current color from (x1,y1) to (x2,y2).
  109. \*----------------------------------------------------------------------*/
  110.  
  111. void 
  112. amiwn_line(PLStream *pls, PLSHORT x1a, PLSHORT y1a, PLSHORT x2a, PLSHORT y2a)
  113. {
  114.     int x1=x1a, y1=y1a, x2=x2a, y2=y2a;
  115.     short comm, xshrt, yshrt;
  116.  
  117.     /* Check for intuition messages */
  118.     /* Note -- very little overhead in this call */
  119.     procmess();
  120.     if (x1 == dev->xold && y1 == dev->yold) {
  121.     comm = PEND;
  122.     xshrt = x2;
  123.     yshrt = y2;
  124.     if (fbuffer) {
  125.         (void) fwrite((char *) &comm, sizeof(short), 1, PlotFile);
  126.         (void) fwrite((char *) &xshrt, sizeof(short), 1, PlotFile);
  127.         (void) fwrite((char *) &yshrt, sizeof(short), 1, PlotFile);
  128.     }
  129.     PLDraw((PLWidth * x2) / InitPLWidth, (PLHeight * y2) / InitPLHeight);
  130.     }
  131.     else {
  132.     comm = PENU;
  133.     xshrt = x1;
  134.     yshrt = y1;
  135.     if (fbuffer) {
  136.         (void) fwrite((char *) &comm, sizeof(short), 1, PlotFile);
  137.         (void) fwrite((char *) &xshrt, sizeof(short), 1, PlotFile);
  138.         (void) fwrite((char *) &yshrt, sizeof(short), 1, PlotFile);
  139.     }
  140.     comm = PEND;
  141.     xshrt = x2;
  142.     yshrt = y2;
  143.     if (fbuffer) {
  144.         (void) fwrite((char *) &comm, sizeof(short), 1, PlotFile);
  145.         (void) fwrite((char *) &xshrt, sizeof(short), 1, PlotFile);
  146.         (void) fwrite((char *) &yshrt, sizeof(short), 1, PlotFile);
  147.     }
  148.     PLMove((PLWidth * x1) / InitPLWidth, (PLHeight * y1) / InitPLHeight);
  149.     PLDraw((PLWidth * x2) / InitPLWidth, (PLHeight * y2) / InitPLHeight);
  150.     }
  151.     dev->xold = x2;
  152.     dev->yold = y2;
  153. }
  154.  
  155. /*----------------------------------------------------------------------*\
  156. * amiwn_polyline()
  157. *
  158. * Draw a polyline in the current color.
  159. \*----------------------------------------------------------------------*/
  160.  
  161. void 
  162. amiwn_polyline (PLStream *pls, PLSHORT *xa, PLSHORT *ya, PLINT npts)
  163. {
  164.     PLINT i;
  165.  
  166.     for (i=0; i<npts-1; i++) 
  167.       amiwn_line( pls, xa[i], ya[i], xa[i+1], ya[i+1] );
  168. }
  169.  
  170. /*----------------------------------------------------------------------*\
  171. * amiwn_clear()
  172. *
  173. * Clear page. 
  174. \*----------------------------------------------------------------------*/
  175.  
  176. void 
  177. amiwn_clear(PLStream *pls)
  178. {
  179.     beepw();
  180.     setpen(0);
  181.     RectFill(PLWRPort, XOffset, YOffset, PLWidth + XOffset, PLHeight + YOffset);
  182.     setpen(pls->icol);
  183.     if (fbuffer) {
  184.     fclose(PlotFile);
  185.     remove(PLOTBFFR);
  186.     }
  187. }
  188.  
  189. /*----------------------------------------------------------------------*\
  190. * amiwn_page()
  191. *
  192. * Set up for the next page.  
  193. * Advance to next family file if necessary (file output).
  194. \*----------------------------------------------------------------------*/
  195.  
  196. void 
  197. amiwn_page(PLStream *pls)
  198. {
  199.     fbuffer = 0;
  200.     if (PLCurPrefs.WinType & PLBUFF) {
  201.     PlotFile = fopen(PLOTBFFR, "w+");
  202.     if (PlotFile == NULL) {
  203.         fprintf(stderr, "\nError opening plot data storage file.\n");
  204.         ClosePLWind();
  205.         CloseLibs();
  206.         exit(1);
  207.     }
  208.     fbuffer = 1;
  209.     }
  210.     dev->xold = UNDEFINED;
  211.     dev->yold = UNDEFINED;
  212.     pls->page++;
  213. }
  214.  
  215. /*----------------------------------------------------------------------*\
  216. * amiwn_adv()
  217. *
  218. * Advance to the next page.
  219. \*----------------------------------------------------------------------*/
  220.  
  221. void 
  222. amiwn_adv(PLStream *pls)
  223. {
  224.     amiwn_clear(pls);
  225.     amiwn_page(pls);
  226. }
  227.  
  228. /*----------------------------------------------------------------------*\
  229. * amiwn_tidy()
  230. *
  231. * Close graphics file or otherwise clean up.
  232. \*----------------------------------------------------------------------*/
  233.  
  234. void 
  235. amiwn_tidy(PLStream *pls)
  236. {
  237.     beepw();
  238.     ClosePLWind();
  239.     CloseLibs();
  240.     if (fbuffer) {
  241.     fclose(PlotFile);
  242.     remove(PLOTBFFR);
  243.     }
  244.     pls->page = 0;
  245.     pls->OutFile = NULL;
  246. }
  247.  
  248. /*----------------------------------------------------------------------*\
  249. * amiwn_color()
  250. *
  251. * Set pen color.
  252. \*----------------------------------------------------------------------*/
  253.  
  254. void 
  255. amiwn_color(PLStream *pls)
  256. {
  257.     short shcol, comm;
  258.  
  259.     if (pls->icol >= 0 && pls->icol < 16) {
  260.     shcol = pls->icol;
  261.     comm = SPEN;
  262.     if (fbuffer) {
  263.         fwrite((char *) &comm, sizeof(short), 1, PlotFile);
  264.         fwrite((char *) &shcol, sizeof(short), 1, PlotFile);
  265.     }
  266.     if (pls->icol >= MaxColors)
  267.         pls->icol = 1;
  268.     CurColor = pls->icol;
  269.     setpen(CurColor);
  270.     }
  271. }
  272.  
  273. /*----------------------------------------------------------------------*\
  274. * amiwn_text()
  275. *
  276. * Switch to text mode.
  277. \*----------------------------------------------------------------------*/
  278.  
  279. void 
  280. amiwn_text(PLStream *pls)
  281. {
  282. }
  283.  
  284. /*----------------------------------------------------------------------*\
  285. * amiwn_graph()
  286. *
  287. * Switch to graphics mode.
  288. \*----------------------------------------------------------------------*/
  289.  
  290. void 
  291. amiwn_graph(PLStream *pls)
  292. {
  293. }
  294.  
  295. /*----------------------------------------------------------------------*\
  296. * amiwn_width()
  297. *
  298. * Set pen width.
  299. \*----------------------------------------------------------------------*/
  300.  
  301. void 
  302. amiwn_width(PLStream *pls)
  303. {
  304.     short shwid, comm;
  305.  
  306.     if (pls->width >= 1 && pls->width <= 3) {
  307.     comm = PWID;
  308.     shwid = pls->width;
  309.     if (fbuffer) {
  310.         fwrite((char *) &comm, sizeof(short), 1, PlotFile);
  311.         fwrite((char *) &shwid, sizeof(short), 1, PlotFile);
  312.     }
  313.     }
  314. }
  315.  
  316. /*----------------------------------------------------------------------*\
  317. * amiwn_esc()
  318. *
  319. * Escape function.
  320. \*----------------------------------------------------------------------*/
  321.  
  322. void 
  323. amiwn_esc(PLStream *pls, PLINT op, char *ptr)
  324. {
  325. }
  326.  
  327. /*----------------------------------------------------------------------*\
  328. * Utility functions
  329. \*----------------------------------------------------------------------*/
  330.  
  331. void 
  332. setlimits(void)
  333. {
  334.     XOffset = PLWindow->BorderLeft + 8;
  335.     YOffset = PLWindow->BorderTop + 8;
  336.     PLWidth = PLWindow->Width - XOffset - PLWindow->BorderRight - 8;
  337.     PLHeight = PLWindow->Height - YOffset - PLWindow->BorderBottom - 8;
  338.     if (PLCurPrefs.WinType & PLASP) {
  339.     if (PLWidth * MaxPLHeight > PLHeight * MaxPLWidth) {
  340.         PLWidth = (MaxPLWidth * PLHeight) / MaxPLHeight;
  341.         XOffset += (PLWindow->Width - PLWidth - PLWindow->BorderLeft -
  342.             PLWindow->BorderRight) / 2;
  343.     }
  344.     else {
  345.         PLHeight = (MaxPLHeight * PLWidth) / MaxPLWidth;
  346.         YOffset += (PLWindow->Height - PLHeight - PLWindow->BorderTop -
  347.             PLWindow->BorderBottom) / 2;
  348.     }
  349.     }
  350. }
  351.  
  352. void 
  353. OpenLibs(void)
  354. {
  355.     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 0L);
  356.     if (IntuitionBase == NULL) {
  357.     puts("\nError opening Intuition library.");
  358.     goto IntError;
  359.     }
  360.  
  361.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L);
  362.     if (GfxBase == NULL) {
  363.     puts("\nError opening Graphics library.");
  364.     goto GfxError;
  365.     }
  366.  
  367.     return;
  368.  
  369.   GfxError:
  370.     CloseLibrary((struct Library *) IntuitionBase);
  371.   IntError:
  372.     pl_exit();
  373. }
  374.  
  375. void 
  376. CloseLibs(void)
  377. {
  378.     CloseLibrary((struct Library *) GfxBase);
  379.     CloseLibrary((struct Library *) IntuitionBase);
  380. }
  381.  
  382. void 
  383. PLMove(PLINT x, PLINT y)
  384. {
  385.     PLINT xsc, ysc;
  386.  
  387.     xsc = XOffset + x;
  388.     ysc = YOffset + PLHeight - y;
  389.     Move(PLWRPort, xsc, ysc);
  390. }
  391.  
  392. void 
  393. PLDraw(PLINT x, PLINT y)
  394. {
  395.     PLINT xsc, ysc;
  396.  
  397.     xsc = XOffset + x;
  398.     ysc = YOffset + PLHeight - y;
  399.     Draw(PLWRPort, xsc, ysc);
  400. }
  401.  
  402. void 
  403. remakeplot(void)
  404. {
  405.     long cxy, x1, y1;
  406.     long x, y;
  407.     void setpen();
  408.  
  409.     setpen(0);
  410.     RectFill(PLWRPort, PLWindow->BorderLeft, PLWindow->BorderTop,
  411.          PLWindow->Width - PLWindow->BorderRight,
  412.          PLWindow->Height - PLWindow->BorderBottom);
  413.  
  414.     setpen(1);
  415.  
  416.     if (fbuffer == 0)
  417.     return;
  418.     prepupdate();
  419.  
  420.     while (!getpoint(&cxy, &x1, &y1)) {
  421.     if (cxy == PENU) {
  422.         x = ((long) PLWidth * (long) x1) / InitPLWidth;
  423.         y = ((long) PLHeight * (long) y1) / InitPLHeight;
  424.         PLMove((PLINT) x, (PLINT) y);
  425.     }
  426.     else if (cxy == PEND) {
  427.         x = ((long) PLWidth * (long) x1) / InitPLWidth;
  428.         y = ((long) PLHeight * (long) y1) / InitPLHeight;
  429.         PLDraw((PLINT) x, (PLINT) y);
  430.     }
  431.     else if (cxy == SPEN) {
  432.         CurColor = x1;
  433.         if (x1 >= MaxColors)
  434.         CurColor = 1;
  435.         setpen((PLINT) CurColor);
  436.     }
  437.     }
  438.  
  439.     finiupdate();
  440.     DisplayBeep(PLScreen);
  441. }
  442.  
  443. static void 
  444. setpen(PLINT color)
  445. {
  446.     SetAPen(PLWRPort, color);
  447. }
  448.  
  449. static void 
  450. beepw(void)
  451. {
  452.     DisplayBeep(PLScreen);
  453.     eventwait();
  454. }
  455.